<template>
    <el-dialog
      title="分配角色"
      :close-on-click-modal="true"
      :visible.sync="visible">
      <el-select v-model="allocRoleIds" multiple placeholder="请选择" size="small" style="width: 80%">
        <el-option
          v-for="item in allRoleList"
          :key="item.id"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="handleAllocDialogConfirm()" size="small">确 定</el-button>
      </span>
    </el-dialog>
</template>

<script>
  import {fetchAllRoleList} from '@/api/ums/role';
  import {getRoleByAdmin,allocRole} from '@/api/ums/admin';

  export default {
    name: "allocRole",
    data() {
      return {
        visible: false,
        allocRoleIds:[],
        allRoleList:[],
        allocAdminId:null
      };
    },
    created() {

    },
    methods: {
      //初始化
      init (id) {
        this.allocAdminId = id;
        this.getAllRoleList();
        this.getRoleListByAdmin(this.allocAdminId);
        this.visible = true
      },
      handleAllocDialogConfirm(){
        this.$confirm('是否要确认?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let params = new URLSearchParams();
          params.append("adminId", this.allocAdminId);
          params.append("roleIds", this.allocRoleIds);
          allocRole(params).then(response => {
            this.$message({
              message: '分配成功！',
              type: 'success',
              onClose: () => {
                this.visible = false;
                this.$emit('refreshDataList')
              }
            });
          })
        })
      },
      getAllRoleList() {
        fetchAllRoleList().then(response => {
          this.allRoleList = response.data;
        });
      },
      getRoleListByAdmin(adminId) {
        getRoleByAdmin(adminId).then(response => {
          let allocRoleList = response.data;
          this.allocRoleIds=[];
          if(allocRoleList!=null&&allocRoleList.length>0){
            for(let i=0;i<allocRoleList.length;i++){
              this.allocRoleIds.push(allocRoleList[i].id);
            }
          }
        });
      }
    }
  }
</script>

<style scoped>

</style>
